/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>
#include "sle_ssap_server_interface.h"
#include "kh_sle_ssap_server.h"
#include "cmd_input.h"
#include <vector>
#include <memory>
#include <cstring>
#include <iterator>

namespace OHOS {
namespace NearLink {
namespace SleStandard {
namespace {
using namespace testing::ext;
} // namespace

class SsapsHandlerTest : public testing::Test {
public:
    static void SetUpTestCase();
    static void TearDownTestCase();
    void SetUp();
    void TearDown();
};

uint8_t g_serverId;
void SsapsHandlerTest::SetUpTestCase() {}

void SsapsHandlerTest::TearDownTestCase() {}

void SsapsHandlerTest::SetUp() {
    SleErrorCode ret = InitSleSsapsService();
    printf("ret = %d", ret);
}

void SsapsHandlerTest::TearDown() {}

HWTEST_F(SsapsHandlerTest, AddSsapServerTest, TestSize.Level1)
{
    SleUuid appUuid;
    appUuid.len = 1;
    appUuid.uuid[0] = 1;
    uint8_t serverId = 255;
    SleErrorCode ret = AddSsapServer(&appUuid, &serverId);
    EXPECT_EQ(ret, SLE_NO_ERROR);
}

HWTEST_F(SsapsHandlerTest, RemoveSsapServerTest, TestSize.Level1)
{
    uint16_t serverId = 255;
    SleErrorCode ret = RemoveSsapServer(serverId);
    EXPECT_EQ(ret, SLE_ERR_INVALID_PARAM);
}

HWTEST_F(SsapsHandlerTest, AddServiceTest, TestSize.Level1)
{
    uint16_t serviceHandle;
    SleUuid serviceUuid;
    serviceUuid.len = 1;
    serviceUuid.uuid[0] = 0;
    bool isPrimary = true;
    SleErrorCode ret = AddService(g_serverId, &serviceUuid, isPrimary, &serviceHandle);
    EXPECT_EQ(ret, SLE_ERR_INTERNAL_WRONG);
}

HWTEST_F(SsapsHandlerTest, AddPropertyTest, TestSize.Level1)
{
    SsapsPropertyParam param = {
        .permissions = SSAP_PERMISSION_CAN_READ | SSAP_PERMISSION_CAN_WRITE,
        .opIndication = SSAP_OPERATEIND_BIT_WRITE | SSAP_OPERATEIND_BIT_NOTIFY,

    };
    uint16_t propertyHandle;
    param.uuid.len = 1;
    param.uuid.uuid[0] = 1;
    uint16_t serviceHandle = (uint8_t)0;
    param.permissions = (uint8_t)0;
    param.opIndication = (uint8_t)0;
    SleErrorCode ret = AddProperty(g_serverId, serviceHandle, &param, &propertyHandle);

    EXPECT_EQ(ret, SLE_ERR_INTERNAL_WRONG);
}

HWTEST_F(SsapsHandlerTest, AddDescriptorTest, TestSize.Level1)
{
    const char *valueStr = "SleTestProperty";
    SsapsDescriptorParam descParam = {
        .permissions = SSAP_PERMISSION_CAN_READ | SSAP_PERMISSION_CAN_WRITE,
        .operateInd = SSAP_OPERATEIND_BIT_WRITE_NO_RSP,
        .type = SSAP_DESCRIPTOR_TYPE_USER_DESCRIPTION_TYPE,
        .valueLen = strlen(valueStr),
        .value = new uint8_t[descParam.valueLen + 1],
    };
    descParam.uuid.len = 1;
    descParam.uuid.uuid[0] = 1;
    uint16_t serviceHandle = 0;
    uint16_t propertyHandle = 0;
    descParam.permissions = (uint8_t)0;
    descParam.operateInd = (uint8_t)0;
    uint16_t descHandle;
    SleErrorCode ret = AddDescriptor(g_serverId, serviceHandle, propertyHandle, &descParam, &descHandle);
    EXPECT_EQ(ret, SLE_ERR_INTERNAL_WRONG);
}

HWTEST_F(SsapsHandlerTest, RemoveAllServicesTest, TestSize.Level1)
{
    SleErrorCode ret = RemoveAllServices(g_serverId);
    (void)ret;
    // EXPECT_EQ(ret, SLE_ERR_FAILED);
}

HWTEST_F(SsapsHandlerTest, StartServiceTest, TestSize.Level1)
{
    uint16_t serviceHandle = 10;
    SleErrorCode ret = StartService(g_serverId, serviceHandle);
    (void)ret;
    // EXPECT_EQ(ret, SLE_ERR_FAILED);
}

HWTEST_F(SsapsHandlerTest, SendResponseTest, TestSize.Level1)
{
    uint16_t connectId = (uint16_t)255;
    SsapsSendRspParam param;
    param.requestId = (uint16_t)124;
    param.status = 0;
    uint8_t value[256];
    const char* str = "nearlink test";
    memcpy(value, str, sizeof(value));
    param.value = value;
    param.valueLen = strlen(reinterpret_cast<const char*>(value)) + 1;
    SleErrorCode ret = SendResponse(g_serverId, connectId, &param);
    EXPECT_EQ(ret, SLE_ERR_INTERNAL_WRONG);
}

HWTEST_F(SsapsHandlerTest, NotifyIndicateTest, TestSize.Level1)
{
    uint16_t connectId = (uint16_t)255;
    SsapsNotifyParam param;
    param.handle = (uint16_t)0;
    param.type = (SsapPropertyType)0;
    uint8_t value[256];
    const char* str = "nearlink test";
    memcpy(value, str, sizeof(value));
    param.value = value;
    param.valueLen = strlen(reinterpret_cast<const char*>(value)) + 1;
    SleErrorCode ret = NotifyIndicate(g_serverId, connectId, &param);
    EXPECT_EQ(ret, SLE_ERR_INTERNAL_WRONG);
}

HWTEST_F(SsapsHandlerTest, NotifyIndicateByUuidTest, TestSize.Level1)
{
    uint16_t connectId = (uint16_t)255;
    SsapsNotyfyByUuidParam param;
    param.uuid.len = 0;
    param.uuid.uuid[0] = 1;
    param.startHandle = (uint16_t)0;
    param.endHandle = (uint16_t)0;
    param.type = (SsapPropertyType)0;
    uint8_t value[256];
    std::memset(value, 0, sizeof(value));
    param.value = value;
    param.valueLen = strlen(reinterpret_cast<const char*>(value)) + 1;
    SleErrorCode ret = NotifyIndicateByUuid(g_serverId, connectId, &param);
    EXPECT_EQ(ret, SLE_ERR_INTERNAL_WRONG);
}

HWTEST_F(SsapsHandlerTest, SetServerMtuInfoTest, TestSize.Level1)
{
    SsapMtuInfo mtuInfo;
    mtuInfo.mtuSize = (uint32_t)256;
    mtuInfo.version = 0;
    SleErrorCode ret = SetServerMtuInfo(g_serverId, &mtuInfo);
    (void)ret;
    // EXPECT_EQ(ret, SLE_ERR_FAILED);
}
}
} // namespace NetManagerStandard
} // namespace OHOS